﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EzyGoEzySystem.Interfaces;
using EzyGoEzySystem.Models;
using System.Collections;
using System.Collections.ObjectModel;

namespace EzyGoEzySystem.Services
{
    /// <summary>
    /// implement ILoginService
    /// </summary>
    /// <author>Hoang Nguyen Nhat Tao</author>
    public class LoginService : ILoginService
    {
        //public IEnumerable<Account> FetchAccount(IEnumerable<string> phoneNumbers)
        //{
        //    // create a collection of accounts
        //    List<Account> accounts = new List<Account>();

        //    // create new database context
        //    var dbContext = new EzyGoEzyDBContext();

        //    // get the account from each phone number
        //    foreach (var phoneNumber in phoneNumbers)
        //    {
        //        // get list of accounts with specified phone number (only one account in the list in this case)
        //        var result = from account in dbContext.Accounts
        //                       where account.PhoneNumber == phoneNumber
        //                       select account;

        //        // check if the list is not empty
        //        if (result.Count() == 0)
        //        {
        //            continue;
        //        }

        //        // get the first account in the list
        //        var firstAccount = result.First();

        //        // add the account to the returned list
        //        accounts.Add(firstAccount);
        //    }

        //    // return the collection of accounts
        //    return accounts;
        //}

        public IEnumerable<Account> FetchAccount(IEnumerable<string> macAddresses, EzyGoEzyDBContext dbContext)
        {
            // create a collection of accounts
            List<Account> accounts = new List<Account>();

            // create new database context
            //var dbContext = new EzyGoEzyDBContext();

            // get the account from each phone number
            foreach (var macAddress in macAddresses)
            {
                // get list of accounts with specified phone number (only one account in the list in this case)
                var result = from account in dbContext.Accounts
                             where account.MacAddress == macAddress
                             select account;

                // check if the list is not empty
                if (result.Count() == 0)
                {
                    continue;
                }

                // get the first account in the list
                var firstAccount = result.First();

                // add the account to the returned list
                accounts.Add(firstAccount);
            }

            // cleanup dbContext
            //dbContext.Dispose();

            // return the collection of accounts
            return accounts;
        }

        public IEnumerable<Account> FetchAccount(IEnumerable<MacAddressPhoneNo> macPhoneInfos)
        {

            // create a collection of accounts
            List<Account> accounts = new List<Account>();

            // create new database context
            var dbContext = new EzyGoEzyDBContext();

            // get the account from each phone number
            foreach (var macPhoneInfo in macPhoneInfos)
            {
                // get list of accounts with specified phone number (only one account in the list in this case)
                var result = from account in dbContext.Accounts
                             where account.PhoneNumber == macPhoneInfo.PhoneNumber
                             select account;

                // check if the list is not empty
                if (result.Count() == 0)
                {
                    continue;
                }

                // get the first account in the list
                var firstAccount = result.First();

                // update MAC Address if different
                if (firstAccount.MacAddress != macPhoneInfo.MacAddress)
                {
                    firstAccount.MacAddress = macPhoneInfo.MacAddress;
                    dbContext.SaveChanges();
                }
                
                // add the account to the returned list
                accounts.Add(firstAccount);
            }

            // cleanup dbContext
            dbContext.Dispose();

            // return the collection of accounts
            return accounts;
        }

        public IEnumerable<Account> FetchAccount(IEnumerable<LoginInformation> loginInfos)
        {
            // create a collection of accounts
            List<Account> accounts = new List<Account>();

            // create new database context
            var dbContext = new EzyGoEzyDBContext();

            // get the account from each phone number
            foreach (var loginInfo in loginInfos)
            {
                // get list of accounts with specified phone number (only one account in the list in this case)
                var result = from account in dbContext.Accounts
                             where account.Email == loginInfo.Email && account.Password == Encryptor.Encrypt(loginInfo.Password)
                             select account;

                // check if the list is not empty
                if (result.Count() == 0)
                {
                    continue;
                }

                // get the first account in the list
                var firstAccount = result.First();

                // add the account to the returned list
                accounts.Add(firstAccount);
            }

            // cleanup dbContext
            dbContext.Dispose();

            // return the collection of accounts
            return accounts;
        }
    }
}